package servlet.user;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import dao.UserDao;
import entity.User;

public class QueryAllUsersServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8009776323979999884L;
	private List<Map<String, String>> list = null;
	private String deptName;
	private String userName;
	private String category;
	private String status;
	private String cnName;
	private String startDate;
	private String endDate;
	
	public QueryAllUsersServlet() {
		super();
	}

	public void destroy() {
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		deptName = request.getParameter("deptName");
		userName = request.getParameter("userName");
		category = request.getParameter("category");
		status = request.getParameter("status");
		cnName = request.getParameter("cnName");
		startDate = request.getParameter("startDate");
		endDate = request.getParameter("endDate");

		UserDao userDao = new UserDao();
		setCondition();
		List<User> users = userDao.queryUsers(list);
		StringBuffer userJson =new StringBuffer();		
		
		if(users != null && users.size() > 0) {
			userJson.append("{\"retCode\":\"1\",");
			userJson.append("\"users\":[");
			// obj to json
			for (User user : users) {
				userJson.append("{");
				userJson.append("\"actTimes\": \"").append(user.getActTimes()).append("\",");
				userJson.append("\"adminName\": \"").append(user.getAdminName()).append("\",");
				userJson.append("\"category\": \"").append(user.getCategory()).append("\",");
				userJson.append("\"cnName\": \"").append(user.getCnname()).append("\",");
				userJson.append("\"deptName\": \"").append(user.getDeptName()).append("\",");
				userJson.append("\"email\": \"").append(user.getEmail()).append("\",");
				userJson.append("\"loginTimes\": \"").append(user.getLoginTimes()).append("\",");
				userJson.append("\"password\": \"").append(user.getPassword()).append("\",");
				userJson.append("\"regTime\": \"").append(user.getRegTime().toString()).append("\",");
				userJson.append("\"requestTimes\": \"").append(user.getRequestTimes()).append("\",");
				userJson.append("\"status\": \"").append(user.getStatus()).append("\",");
				userJson.append("\"userName\": \"").append(user.getUserName()).append("\"");
				userJson.append("},");
			}
			userJson.deleteCharAt(userJson.length() - 1).append("]}");
		} else {
			userJson.append("{\"retCode\":\"0\",");
			userJson.append("\"desc\":\"无记录\"}");
		}
		
		out.print(userJson.toString());
		out.close();
	}
	
	private void setCondition() throws UnsupportedEncodingException {
		list = new ArrayList<Map<String, String>>();
		if(!StringUtils.isEmpty(deptName)) {
			deptName = URLDecoder.decode(URLDecoder.decode(deptName, "UTF-8"), "UTF-8");
			Map<String, String> map = new HashMap<String, String>();
			map.put("属性", "deptName");
			map.put("值", deptName);
			list.add(map);
		}
		if(!StringUtils.isEmpty(userName)) {
			userName = URLDecoder.decode(URLDecoder.decode(userName, "UTF-8"), "UTF-8");
			Map<String, String> map = new HashMap<String, String>();
			map.put("属性", "username");
			map.put("值", userName);
			list.add(map);
		}
		if(!StringUtils.isEmpty(category)) {
			category = URLDecoder.decode(URLDecoder.decode(category, "UTF-8"), "UTF-8");
			Map<String, String> map = new HashMap<String, String>();
			map.put("属性", "category");
			map.put("值", category);
			list.add(map);
		}
		if(!StringUtils.isEmpty(status)) {
			status = URLDecoder.decode(URLDecoder.decode(status, "UTF-8"), "UTF-8");
			Map<String, String> map = new HashMap<String, String>();
			map.put("属性", "status");
			map.put("值", status);
			list.add(map);
		}
		if(!StringUtils.isEmpty(cnName)) {
			cnName = URLDecoder.decode(URLDecoder.decode(cnName, "UTF-8"), "UTF-8");
			Map<String, String> map = new HashMap<String, String>();
			map.put("属性", "cnname");
			map.put("值", cnName);
			list.add(map);
		}
		if(!StringUtils.isEmpty(startDate)) {
			cnName = URLDecoder.decode(URLDecoder.decode(startDate, "UTF-8"), "UTF-8");
			Map<String, String> map = new HashMap<String, String>();
			map.put("属性", "startDate");
			map.put("值", startDate);
			list.add(map);
		}
		if(!StringUtils.isEmpty(endDate)) {
			cnName = URLDecoder.decode(URLDecoder.decode(endDate, "UTF-8"), "UTF-8");
			Map<String, String> map = new HashMap<String, String>();
			map.put("属性", "endDate");
			map.put("值", endDate);
			list.add(map);
		}
	}
	
	public void init() throws ServletException {
	}

}
